package com.tanhua.server.service;

import com.tanhua.autoconfig.template.AipFaceTemplate;
import com.tanhua.autoconfig.template.QiNiuTemplate;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private UserLikeApi userLikeApi;

    @Autowired
    private QiNiuTemplate qiNiuTemplate;

    @Autowired
    private AipFaceTemplate aipFaceTemplate;


    // 保存
    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }

    /*public void update(UserInfo userInfo){
        userInfoApi.update(userInfo);
    }*/

    // 更新用户头像
    public void updateHead(MultipartFile headPhoto, Long id){
        try {
//            InputStream inputStream = headPhoto.getInputStream();
            // 1:将图片上传到七牛云上
            String images = qiNiuTemplate.upload(headPhoto.getBytes(),headPhoto.getOriginalFilename());
            // 2:调用百度判断是否包含人脸
            boolean detect = aipFaceTemplate.detect(images);
            // 3:如果不包含人脸,直接抛出一个异常
            if (!detect){
                throw new BusinessException(ErrorResult.faceError());
            }else{
                // 4:如果包含人脸,调用API更新
                // 根据id更新其中的头像字段
                UserInfo userInfo = new UserInfo();
                userInfo.setId(Long.valueOf(id));
                userInfo.setAvatar(images);
                userInfoApi.update(userInfo);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 根据用户id查询
    public UserInfoVo findById(Long userId) {
        // 根据id查询userInfo对象
        UserInfo user = userInfoApi.findById(userId);
        UserInfoVo vo = new UserInfoVo();
        // 将userInfo中的属性,拷贝到vo中去, 只会拷贝同名同类型的属性
        BeanUtils.copyProperties(user,vo);
        if (user.getAge() != null) {
            vo.setAge(user.getAge().toString());
        }
        return vo;
    }

    // 修改用户详情
    public void updateById(UserInfo userInfo) {
        userInfoApi.updateById(userInfo);
    }


    // 查询数量统计
    public Map<String,Integer> findCounts() {
        Map<String,Integer> map = new HashMap<>();
        Long userId = UserHolder.getUserId();
        // 1:查询喜欢我的
        Integer fanCount = userLikeApi.likMe(userId,true);
        // 2:查询我喜欢的
        Integer loveCount = userLikeApi.myLike(userId,true);
        // 3:查询互相喜欢的
        Integer eachLoveCount = userLikeApi.eachLoveCount(userId,true);
        map.put("eachLoveCount",eachLoveCount);
        map.put("loveCount",loveCount);
        map.put("fanCount",fanCount);
        return map;
    }
}
